// 蜂窝蜜造平台生成代码，如手工更改，请添加到 .beeignore 忽略生成
package com.fowo.api.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fowo.api.common.model.IdListPo;
import com.fowo.api.common.model.IdPo;
import com.fowo.api.common.model.ImportRow;
import com.fowo.api.common.model.OptionItem;
import com.fowo.api.common.model.R;
import com.fowo.api.common.util.ResponseUtils;
import com.fowo.api.entity.Platformshipping;
import com.fowo.api.model.platformshipping.PlatformshippingImportPo;
import com.fowo.api.model.platformshipping.PlatformshippingItemVo;
import com.fowo.api.model.platformshipping.PlatformshippingSearchParamPo;
import com.fowo.api.model.platformshipping.PlatformshippingVo;
import com.fowo.api.service.PlatformshippingService;
import com.fowo.api.sys.entity.SysFile;
import com.fowo.api.user.model.JwtUserInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import org.springframework.core.io.ClassPathResource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StreamUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@Tag(name = "平台仓发货单")
@RequestMapping("/api/platformshipping")
@RestController
public class PlatformshippingController {

  @Resource
  private PlatformshippingService service;

  @Operation(summary = "查询结果")
  @GetMapping
  public R<List<PlatformshippingItemVo>> search(
    PlatformshippingSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.search(search));
  }

  @Operation(summary = "快速查询选项(有缓存)")
  @GetMapping("options")
  public R<List<OptionItem<PlatformshippingItemVo>>> searchOptions(
    PlatformshippingSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.searchOptions(search));
  }

  @Operation(summary = "查询最后更新时间")
  @GetMapping("lastUpdateTime")
  public R<Date> searchLastUpdateTime(PlatformshippingSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.searchLastUpdateTime(search));
  }

  @Operation(summary = "获取分页查询结果")
  @GetMapping("page")
  public R<Page<PlatformshippingItemVo>> getPage(
    PlatformshippingSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.pageSearch(search));
  }

  @Operation(summary = "获取包括子表的查询结果")
  @GetMapping("page-with-child")
  public R<Page<?>> getPageWithChild(PlatformshippingSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.pageWithChildSearch(search));
  }

  @Operation(summary = "仅获取子表的查询结果")
  @GetMapping("page-only-child/{parentId}")
  public R<Page<?>> getPageOnlyChild(
    @PathVariable Long parentId,
    PlatformshippingSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.pageOnlyChildSearch(parentId, search));
  }

  @Operation(summary = "查询是否后台下载")
  @GetMapping("getBackgroundExportFlag")
  public R<Boolean> getBackgroundExportFlag(
    PlatformshippingSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.getBackgroundExportFlag(search));
  }

  @Operation(summary = "获取详情")
  @GetMapping("{id}")
  public R<PlatformshippingVo> getVoById(@PathVariable Long id) {
    return R.data(() -> service.getVoById(id));
  }

  @Operation(summary = "获取发货单号相关的编号列表")
  @GetMapping("shipment-code/{texts}/ids")
  public R<List<Long>> getIdsByShipmentCode(@PathVariable String texts) {
    List<String> nameList = Arrays
      .stream(texts.split(","))
      .collect(Collectors.toList());
    return R.data(() -> service.getIdsByShipmentCode(nameList));
  }

  @Operation(summary = "获取编号相关的发货单号列表")
  @GetMapping("shipment-code/{ids}")
  public R<List<String>> getVoById(@PathVariable String ids) {
    List<Long> idList = Arrays
      .asList(ids.split(","))
      .stream()
      .map(Long::valueOf)
      .collect(Collectors.toList());
    return R.data(() -> service.getShipmentCodeByIds(idList));
  }

  @PreAuthorize("@am.canAccess('platformshippingCreate')")
  @Operation(summary = "新增")
  @PostMapping
  public R<Long> create(@Validated @RequestBody PlatformshippingVo model) {
    return R.data(() -> service.create(model));
  }

  @PreAuthorize("@am.canAccess('platformshippingEdit')")
  @Operation(summary = "更新")
  @PutMapping
  public R update(@RequestBody PlatformshippingVo model) {
    return R.run(() -> service.update(model));
  }

  @PreAuthorize("@am.canAccess('platformshippingEdit')")
  @Operation(summary = "编辑更新")
  @PutMapping("edit")
  public R updateForEdit(@RequestBody PlatformshippingVo model) {
    return R.run(() -> service.updateForEdit(model));
  }

  @PreAuthorize("@am.canAccess('platformshippingDelete')")
  @Operation(summary = "删除")
  @DeleteMapping("{id}")
  public R delete(@PathVariable Long id) {
    return R.run(() -> service.delete(id));
  }

  @PreAuthorize("@am.canAccess('platformshippingDelete')")
  @Operation(summary = "批量删除")
  @DeleteMapping("batch")
  public R batchDelete(@RequestBody IdListPo po) {
    return R.run(() -> service.batchDelete(po.getIds()));
  }

  @PreAuthorize("@am.canAccess('platformshippingImport')")
  @Operation(summary = "导入预览")
  @PostMapping("import-preview")
  public R<List<ImportRow<PlatformshippingImportPo>>> importPreview(
    @RequestBody SysFile sysFile,
    @RequestParam(name = "template", required = false) Long templateId,
    @RequestParam(
      name = "allowOverrides",
      defaultValue = "false"
    ) boolean allowOverrides
  ) {
    return R.data(() ->
      service.importPreview(sysFile, templateId, allowOverrides)
    );
  }

  @PreAuthorize("@am.canAccess('platformshippingImport')")
  @Operation(summary = "完成导入")
  @PostMapping("import-done")
  public R importDone(
    @RequestBody List<ImportRow<PlatformshippingImportPo>> rows,
    @RequestParam(name = "template", required = false) Long templateId
  ) {
    return R.run(() -> service.importDone(rows, templateId));
  }

  @Operation(summary = "下载导入模版")
  @GetMapping("import/template")
  public void downloadImportTemplate(HttpServletResponse response)
    throws IOException {
    ResponseUtils.setAttachmentFileName(response, "平台仓发货单导入模版.xlsx");
    InputStream inputStream = new ClassPathResource(
      "templates/Platformshipping.xlsx"
    )
      .getInputStream();
    StreamUtils.copy(inputStream, response.getOutputStream());
  }

  @Operation(summary = "导出")
  @GetMapping("export")
  @PreAuthorize("@am.canAccess('platformshippingExport')")
  public void export(
    @RequestParam(name = "template", required = false) Long templateId,
    PlatformshippingSearchParamPo search,
    HttpServletResponse response
  ) throws Exception {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    service.export(templateId, search, response);
  }

  @PreAuthorize("@am.canAccess('platformshippingCancelLockpInventory')")
  @Operation(summary = "取消锁库存_仓位")
  @PostMapping("cancelLockpInventory")
  public R cancelLockpInventory(@RequestBody IdPo idPo) {
    return R.run(() -> service.cancelLockpInventory(idPo.getId()));
  }

  @PreAuthorize("@am.canAccess('platformshippingCancelLocksInventory')")
  @Operation(summary = "取消锁库存_即时")
  @PostMapping("cancelLocksInventory")
  public R cancelLocksInventory(@RequestBody IdPo idPo) {
    return R.run(() -> service.cancelLocksInventory(idPo.getId()));
  }

  @PreAuthorize("@am.canAccess('platformshippingLockStockOut')")
  @Operation(summary = "出库锁定")
  @PostMapping("lockStockOut")
  public R lockStockOut(@RequestBody IdPo idPo) {
    return R.run(() -> service.lockStockOut(idPo.getId()));
  }

  @PreAuthorize("@am.canAccess('platformshippingLockpInventory')")
  @Operation(summary = "锁库存_仓位")
  @PostMapping("lockpInventory")
  public R lockpInventory(@RequestBody IdPo idPo) {
    return R.run(() -> service.lockpInventory(idPo.getId()));
  }

  @PreAuthorize("@am.canAccess('platformshippingLocksInventory')")
  @Operation(summary = "锁库存_即时")
  @PostMapping("locksInventory")
  public R locksInventory(@RequestBody IdPo idPo) {
    return R.run(() -> service.locksInventory(idPo.getId()));
  }

  @PreAuthorize("@am.canAccess('platformshippingCancelOrder')")
  @Operation(summary = "作废")
  @PostMapping("cancelOrder")
  public R cancelOrder(@RequestBody IdPo po) {
    return R.run(() -> service.cancelOrder(po.getId()));
  }

  @PreAuthorize("@am.canAccess('platformshippingReleaseInventory')")
  @Operation(summary = "释放库存")
  @PostMapping("releaseInventory")
  public R releaseInventory(@RequestBody IdListPo po) {
    return R.run(() -> service.releaseInventory(po.getIds()));
  }

  @PreAuthorize("@am.canAccess('platformshippingSendoutgoods')")
  @Operation(summary = "发货")
  @PostMapping("sendoutgoods")
  public R sendoutgoods(@RequestBody IdListPo po) {
    return R.run(() -> service.sendoutgoods(po.getIds()));
  }

  @PreAuthorize("@am.canAccess('platformshippingAllocateInventory')")
  @Operation(summary = "分配库存")
  @PostMapping("allocateInventory")
  public R allocateInventory(@RequestBody IdListPo po) {
    return R.run(() -> service.allocateInventory(po.getIds()));
  }

  @PreAuthorize("@am.canAccess('platformshippingBatchcompletionpicking')")
  @Operation(summary = "批量完成拣货")
  @PostMapping("batchcompletionpicking")
  public R batchcompletionpicking(@RequestBody IdListPo po) {
    return R.run(() -> service.batchcompletionpicking(po.getIds()));
  }

  /**
   * 视图选项卡标题上显示计数
   */
  @Operation(summary = "视图选项卡标题上显示计数")
  @GetMapping("getTabHeaderShowTotal")
  public R<Map> getTabHeaderShowTotal() {
    return R.data(() -> service.getTabHeaderShowTotal());
  }

  @Operation(summary = "全部")
  @GetMapping("all-data")
  public R<List<PlatformshippingItemVo>> allData(
    PlatformshippingSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.allData(search));
  }

  @Operation(summary = "全部分页")
  @GetMapping("all-dataPage")
  public R<Page<PlatformshippingItemVo>> allDataPage(
    PlatformshippingSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.allDataPage(search));
  }

  @Operation(summary = "全部包括子表的查询结果")
  @GetMapping("all-dataPage-with-child")
  public R<Page<?>> allDataPageWithChild(PlatformshippingSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.allDataPageWithChildSearch(search));
  }

  @Operation(summary = "全部最后更新时间")
  @GetMapping("all-dataLastUpdateTime")
  public R<Date> allDataLastUpdateTime(PlatformshippingSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.allDataLastUpdateTime(search));
  }

  @Operation(summary = "待配货")
  @GetMapping("waiting-distribution")
  public R<List<PlatformshippingItemVo>> waitingDistribution(
    PlatformshippingSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.waitingDistribution(search));
  }

  @Operation(summary = "待配货分页")
  @GetMapping("waiting-distributionPage")
  public R<Page<PlatformshippingItemVo>> waitingDistributionPage(
    PlatformshippingSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.waitingDistributionPage(search));
  }

  @Operation(summary = "待配货包括子表的查询结果")
  @GetMapping("waiting-distributionPage-with-child")
  public R<Page<?>> waitingDistributionPageWithChild(
    PlatformshippingSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.waitingDistributionPageWithChildSearch(search));
  }

  @Operation(summary = "待配货最后更新时间")
  @GetMapping("waiting-distributionLastUpdateTime")
  public R<Date> waitingDistributionLastUpdateTime(
    PlatformshippingSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.waitingDistributionLastUpdateTime(search));
  }

  @Operation(summary = "待发货")
  @GetMapping("tobe-shipped")
  public R<List<PlatformshippingItemVo>> tobeShipped(
    PlatformshippingSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.tobeShipped(search));
  }

  @Operation(summary = "待发货分页")
  @GetMapping("tobe-shippedPage")
  public R<Page<PlatformshippingItemVo>> tobeShippedPage(
    PlatformshippingSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.tobeShippedPage(search));
  }

  @Operation(summary = "待发货包括子表的查询结果")
  @GetMapping("tobe-shippedPage-with-child")
  public R<Page<?>> tobeShippedPageWithChild(
    PlatformshippingSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.tobeShippedPageWithChildSearch(search));
  }

  @Operation(summary = "待发货最后更新时间")
  @GetMapping("tobe-shippedLastUpdateTime")
  public R<Date> tobeShippedLastUpdateTime(
    PlatformshippingSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.tobeShippedLastUpdateTime(search));
  }

  @Operation(summary = "已发货")
  @GetMapping("shipped")
  public R<List<PlatformshippingItemVo>> shipped(
    PlatformshippingSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.shipped(search));
  }

  @Operation(summary = "已发货分页")
  @GetMapping("shippedPage")
  public R<Page<PlatformshippingItemVo>> shippedPage(
    PlatformshippingSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.shippedPage(search));
  }

  @Operation(summary = "已发货包括子表的查询结果")
  @GetMapping("shippedPage-with-child")
  public R<Page<?>> shippedPageWithChild(PlatformshippingSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.shippedPageWithChildSearch(search));
  }

  @Operation(summary = "已发货最后更新时间")
  @GetMapping("shippedLastUpdateTime")
  public R<Date> shippedLastUpdateTime(PlatformshippingSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.shippedLastUpdateTime(search));
  }

  @Operation(summary = "已作废")
  @GetMapping("cancellation")
  public R<List<PlatformshippingItemVo>> cancellation(
    PlatformshippingSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.cancellation(search));
  }

  @Operation(summary = "已作废分页")
  @GetMapping("cancellationPage")
  public R<Page<PlatformshippingItemVo>> cancellationPage(
    PlatformshippingSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.cancellationPage(search));
  }

  @Operation(summary = "已作废包括子表的查询结果")
  @GetMapping("cancellationPage-with-child")
  public R<Page<?>> cancellationPageWithChild(
    PlatformshippingSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.cancellationPageWithChildSearch(search));
  }

  @Operation(summary = "已作废最后更新时间")
  @GetMapping("cancellationLastUpdateTime")
  public R<Date> cancellationLastUpdateTime(
    PlatformshippingSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.cancellationLastUpdateTime(search));
  }
}
